Cover TOC Chap Prev Chap Prev Fig Next Fig Next Chap

Chapter 30: Creating Program Scripts

../ch30/30fig04.gif
Figure 30.4

A debug filter used to monitor the output of a TimeSensor node.

30fig04.wrl
Click on the image to view the VRML scene.

#VRML V2.0 utf8
# The VRML 2.0 Sourcebook
# Copyright (c) 1997
# Andrea L. Ames, David R. Nadeau, and John L. Moreland
Group {
    children [
        Background {
            skyColor    [ 1.0 0.0 0.8,  0.5 0.0 0.8,  0.0 0.0 0.8 ]
            skyAngle    [ 1.309, 1.571 ]
            groundColor [ 0.0 0.0 0.1,  0.0 0.1 0.3,  0.3 0.3 0.6 ]
            groundAngle [ 1.309, 1.571 ]
        },
    # Floor
        Shape {
            appearance Appearance {
                material Material { }
            }
            geometry Box { size 2.0 0.01 0.5 }
        },
    # Animating red ball
        Transform {
            translation 0.0 1.1 0.0
            children DEF BallTransform Transform {
                children Shape {
                    appearance Appearance {
                        material Material {
                            diffuseColor 1.0 0.0 0.0
                        }
                    }
                    geometry Sphere { radius 0.1 }
                }
            }
        },
    # Animation clock
        DEF Clock TimeSensor {
            cycleInterval 4.0
            loop TRUE
        },
    # Script
        DEF Mover Script {
            url "javascript:
                function set_fraction( fraction, eventTime ) {
                    value_changed[0] = fraction;
                    value_changed[1] = radius * Math.sin( turns * fraction * 6.28 );
                    value_changed[2] = radius * Math.cos( turns * fraction * 6.28 );
                }"
            field    SFFloat radius 1.0
            field    SFFloat turns  1.0
            eventIn  SFFloat set_fraction
            eventOut SFVec3f value_changed
        }
    # Debugger
        DEF Debug Script {
            url "javascript:
                function initalize( ) {
                    string_changed[0] = label + ':';
                }
                function set_float( f, ts ) {
                    string_changed[0] = label + ': ' + f;
                }"
            field    SFString label "fraction"
            eventIn  SFFloat  set_float
            eventOut MFString string_changed
        },
        Transform {
            translation 0.0 0.01 -0.15
            children Shape {
                appearance Appearance {
                    material Material {
                        diffuseColor  0.0 0.0 0.0
                        emissiveColor 1.0 1.0 1.0
                    }
                }
                geometry DEF Message Text {
                    fontStyle FontStyle {
                        size 0.35
                        justify "MIDDLE"
                    }
                }
            }
        }
    ]
}
ROUTE Clock.fraction_changed  TO Mover.set_fraction
ROUTE Mover.value_changed     TO BallTransform.set_translation
ROUTE Clock.fraction_changed  TO Debug.set_float
ROUTE Debug.string_changed    TO Message.set_string